package com.codeape.number.ui;

import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.codeape.number.R;
import com.codeape.number.base.mvvm.command.Command;
import com.codeape.number.common.view.XPageChangeListener;
import com.codeape.number.databinding.ActivityAnnotationBinding;
import com.codeape.number.ui.fragment.GuaciTab;
import com.codeape.number.ui.fragment.YaociTab;
import com.codeape.number.util.StatusBarUtil;
import com.codeape.number.util.Utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 解卦页
 *
 * @author LiuPeiming
 * Created at old
 */
public class AnnotationActivity extends FragmentActivity {

    ActivityAnnotationBinding mBinding = null;

    /**
     * 回退
     */
    public Command onBackCommand = new Command(this::finish);

    private final List<Fragment> mFragments = new ArrayList<>();

    public ObservableInt picturePosition = new ObservableInt();

    public ObservableField<PagerAdapter> adapter = new ObservableField<>();

    public ObservableField<ViewPager.OnPageChangeListener> changeListener = new ObservableField<>();

    public ObservableField<Drawable> imgGua = new ObservableField<>();
    public ObservableField<Drawable> imgYao = new ObservableField<>();

    /**
     * 切换卦辞Fra
     */
    public Command onGuaciCommand = new Command(() -> picturePosition.set(0));

    /**
     * 切换爻辞Fra
     */
    public Command onYaociCommand = new Command(() -> picturePosition.set(1));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //判断设备是否处于深色模式
        if ((getApplicationContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_YES) != 0) {
            //设置状态栏黑底白字
            StatusBarUtil.changeTitleBarColor(this, ContextCompat.getColor(this, R.color.background));
            StatusBarUtil.setDarkMode(this);
        } else {
            //设置状态栏白底黑字
            StatusBarUtil.changeTitleBarColor(this, ContextCompat.getColor(this, R.color.background));
            StatusBarUtil.setLightMode(this);
        }

        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_annotation);
        mBinding.setActivity(this);

        initFragment();

        imgGua.set(ContextCompat.getDrawable(this, Utils.judgeGuaType(Integer.parseInt((Utils.readSharedPreferences(this, "Config", "gua"))))));
        imgYao.set(ContextCompat.getDrawable(this, Utils.judgeGuaType(Integer.parseInt((Utils.readSharedPreferences(this, "Config", "yao"))))));

    }

    private void initFragment() {

        mFragments.add(new GuaciTab());
        mFragments.add(new YaociTab());

        adapter.set(
                new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
                    @NonNull
                    @Override
                    public Fragment getItem(int position) {
                        return mFragments.get(position);
                    }

                    @Override
                    public int getCount() {
                        return mFragments.size();
                    }
                }
        );

        changeListener.set(new XPageChangeListener(this, mFragments.size(), picturePosition));

        picturePosition.set(1);
        setViewPagerDefaultItem();

    }

    /**
     * 设置ViewPager默认item
     */
    private void setViewPagerDefaultItem() {
        //我这里mViewpager是viewpager子类的实例。如果你是viewpager的实例，也可以这么干。
        try {
            Class<?> c = Class.forName("androidx.viewpager.widget.ViewPager");
            Field field = c.getDeclaredField("mCurItem");
            field.setAccessible(true);
            field.setInt(mBinding.vp, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Objects.requireNonNull(adapter.get()).notifyDataSetChanged();

        mBinding.vp.setCurrentItem(1);
    }

}